/**
 * Created by Administrator on 2017/11/29.
 */
function creatXmlHttpRequest() {
    var request;
    try {
        request = new XMLHttpRequest();
    }catch (e){
        try {
            request = new ActiveXObject("Xsxml2.XMLHTTP")
        }catch (e){
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){

            }
    }
    }
    return request;
}
function checkUsername() {
    var username = document.getElementById("username").value;
    var xmlHttpRequest = creatXmlHttpRequest();
    //get
    /*xmlHttpRequest.open("GET","/ajax/findUserNameServlet?username="+username+"&time="+new Date(),true);
    xmlHttpRequest.send(null);*/
    //post
    xmlHttpRequest.open("POST","/ajax/findUserNameServlet",true);
    xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded")//ajax要设置响应头
    xmlHttpRequest.send("username="+username+"&time="+new Date());


    xmlHttpRequest.onreadystatechange = function () {
        if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status==200 ){
            var flag = xmlHttpRequest.responseText;
            var msg = document.getElementById("msg");
            if(flag=="true"){
                msg.innerText="可以注册";
                msg.style.color="green";
            }else if(flag=="false"){
                msg.innerText="用户名已存在";
                msg.style.color="red";
            }
            if(username==""||username==null){
                msg.innerText="用户名不能为空";
                msg.style.color="red";
            }
        }
    }
}